package edu.gatech.cc.liam.core;

import java.util.HashMap;

public class Histogram extends HashMap<Integer, Integer> {

	private static final long serialVersionUID = 5492264092779404536L;

	public void increment(Integer bucket) {
		Integer value = this.get(bucket);
		if (value == null)
			value = 0;
		this.put(bucket, value+1);
	}
	
	public void increment(Integer bucket, Integer numInc) {
		Integer value = this.get(bucket);
		if (value == null)
			value = 0;
		this.put(bucket, value+numInc);
	}
	
	public void addHist(HashMap<Integer, Integer> hist) {
		for(Integer v : hist.keySet()) {
			this.increment(v, hist.get(v));
		}
	}
	public double getAverage() {
		if(this.isEmpty())
			return Double.NaN;
		double sum = 0.0;
		int count = 0;
		for(Integer v : this.keySet()) {
			sum += v * this.get(v);
			count += this.get(v);
		}
		return sum/count;
	}
}
